<template>
  <!-- 里程数修改申请页面 -->
  <div>
    <!--条件筛选-->
    <TopFilter ref="topFilter" :list="topFilterList" :cols="3" labelWidth="130" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <FilterTable
      ref="table"
      columnsRef="warrantyDate-modify-apply"
      :isSelectColumn="false"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'rows'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectData"
    >
      <template slot="controls">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="addItem">新建</el-button>
        <AjaxButton size="small" type="primary" :click="exportClick">导出</AjaxButton>
      </template>
    </FilterTable>

    <BaseDialog :visible.sync="showAdd" :title="dialogTitle" destroyOnClose :containerStyle="{ height: 'calc(100% - 70px)', overflow: 'auto', paddingBottom: '70px' }">
      <modifyDialog :type="handleType" :row="formData" @close="closeDialog" @saveDialog="saveDialog" />
    </BaseDialog>
    <BaseDialog :visible.sync="action.visible" :title="action.title" destroyOnClose :containerStyle="{ height: 'calc(100% - 70px)', overflow: 'auto', paddingBottom: '70px' }">
      <SearchDialog :type="action.type" :row="action.data" @close="closeDialog" @saveDialog="saveDialog" />
    </BaseDialog>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import dictCode from '@/utils/dictCode';
import { exportExcelFile } from '@/utils';
import { selectWarrantyDateChange, applyWarrantyDateChange, dealerExportWarranty } from '@/api/repair/repairBusiness/warrantyDateModify/index.js';
import modifyDialog from './warrantyDateModifyDialog.vue';
import SearchDialog from './warrantyDateSearchDialog.vue';

// 延保订单查询
export default {
  name: 'warrantyDate-modify-apply',
  components: { modifyDialog, SearchDialog },
  mixins: [dictionary],
  data() {
    return {
      fetchParams: { xhrAbort: false },
      dialogTitle: '',
      fetchApi: selectWarrantyDateChange,
      columns: this.createTableColumns(),
      handleType: '',
      formData: {},
      tableData: [],
      action: { type: '', visible: false, title: '', data: null },
      selectFlag: false,
      topFilterList: this.createTopFilterList(),
      showAdd: false
    };
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  methods: {
    async exportClick() {
      const [err, data] = await this.$refs.topFilter.GET_FORM_DATA();
      dealerExportWarranty(data).then(res => {
        exportExcelFile(res.data, '保修日期变更报告表');
      });
    },
    async closeDialog() {
      this.action.visible = false;
      this.showAdd = false;
      const [err, data] = await this.$refs.topFilter.GET_FORM_DATA();
      this.fetchParams = { ...this.fetchParams, ...data };
    },
    async saveDialog() {
      const [err, data] = await this.$refs.topFilter.GET_FORM_DATA();
      this.fetchParams = { ...this.fetchParams, ...data };
    },
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '保修日期变更申请单号',
          fieldName: 'changeNo',
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请输入...'
        },
        // {
        //   type: 'INPUT',
        //   label: '维修工单号',
        //   fieldName: 'roNo',
        //   placeholder: '请输入...'
        // },
        {
          type: 'SELECT',
          label: '申请状态',
          fieldName: 'status',
          placeholder: '请选择...',
          itemList: this.createDictList('9993')
        },
        {
          type: 'RANGE_DATE',
          label: '申请日期',
          fieldName: 'startDate|stopDate',
          placeholder: '请选择...',
          itemList: []
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          minWidth: '100',
          render: (props, h) => {
            return (
              <div>
                <el-button
                  disabled={props.row.status == '99931002' || props.row.status == '99931004' || props.row.status == '99931005'}
                  size="mini"
                  type="text"
                  onClick={() => this.emitFun(props.row, 'edit')}
                >
                  编辑
                </el-button>

                <el-button size="mini" type="text" v-show={true} onClick={() => this.emitFun(props.row, 'show')}>
                  明细
                </el-button>
              </div>
            );
          }
        },
        {
          title: '保修日期变更申请单号',
          dataIndex: 'changeNo',
          width: 200,
          sorter: true
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          sorter: true,
          width: 200
        },
        {
          title: '车牌号',
          dataIndex: 'license',
          sorter: true
        },
        {
          title: '原保修开始日期',
          dataIndex: 'beforeBeginDate',
          sorter: true,
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '原保修结束日期',
          dataIndex: 'beforeEndDate',
          sorter: true,
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '变更保修开始日期',
          dataIndex: 'afterBeginDate',
          sorter: true,
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '变更保修结束日期',
          dataIndex: 'afterEndDate',
          sorter: true,
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '申请时间',
          dataIndex: 'changeDate',
          width: 150,
          dateFormat: 'yyyy-MM-dd HH:mm:ss',
          sorter: true
        },
        {
          title: '申请状态',
          dataIndex: 'status',
          sorter: true,
          dictItems: this.createDictList('9993')
        },
        {
          title: '申请人',
          dataIndex: 'changeUserName',
          sorter: true,
          width: 200
        },
        {
          title: '申请原因',
          dataIndex: 'remark',
          sorter: true
        }
      ];
    },

    emitFun(val, type) {
      this.handleType = type;
      this.dialogTitle = `保修日期变更申请`;
      this.formData = val;
      if (type == 'show') {
        this.action = { type: type, title: '保修日期变更查询', data: val, visible: true };
      } else {
        this.showAdd = true;
      }
    },

    addItem() {
      // 新建
      this.handleType = 'add';
      this.showHandler();
    },
    showHandler() {
      // 显示
      this.dialogTitle = `保修日期变更申请`;
      this.showAdd = true;
    },
    selectData(val) {
      this.tableData = val;
      if (val.length == 0) {
        this.selectFlag = false;
      } else {
        this.selectFlag = true;
      }
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, this.fetchParams, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    }
  }
};
</script>

<style lang="less" scoped></style>
